-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHttp.cs
94 lines (85 loc) · 2.49 KB
/
Http.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
namespace My
{
using System;
using System.Net;
using System.Text;
/// <summary>
/// HTTP网络请求相关函数
/// </summary>
public sealed partial class Http
{
/// <summary>
/// 获取字符串
/// </summary>
/// <param name="URL">网址链接</param>
/// <returns>结果字符串(失败返回空字符串)</returns>
public static string GetString(string URL)
{
WebClient client = new WebClient();
client.Encoding = Encoding.UTF8;
try
{
return client.DownloadString(URL);
}
catch (Exception ex)
{
return "";
}
}
/// <summary>
/// 获取字符串
/// </summary>
/// <param name="URL">网址链接</param>
/// <param name="Encoding">使用特定的字符编码(默认UTF-8)</param>
/// <returns>结果字符串(失败返回空字符串)</returns>
public static string GetString(string URL, Encoding Encoding)
{
WebClient client = new WebClient();
client.Encoding = Encoding;
try
{
return client.DownloadString(URL);
}
catch (Exception ex)
{
return "";
}
}
/// <summary>
/// 获取字节数组
/// </summary>
/// <param name="URL">网址链接</param>
/// <returns>结果Byte数组(失败返回空Byte数组)</returns>
public static byte[] GetByte(string URL)
{
WebClient client = new WebClient();
try
{
return client.DownloadData(URL);
}
catch (Exception ex)
{
return new byte[0];
}
}
/// <summary>
/// 下载文件
/// </summary>
/// <param name="URL">文件链接</param>
/// <param name="FilePath">保存到的文件路径(可以是相对路径)</param>
/// <returns>是否下载成功</returns>
public static bool DownloadFile(string URL, string FilePath)
{
WebClient client = new WebClient();
try
{
client.DownloadFile(new Uri(URL), FilePath);
return true;
}
catch (Exception ex)
{
return false;
}
}
}
}